package com.snake.springboot.custom.service.impl.base;
import com.snake.springboot.custom.api.model.points.PointsChangeModel;
import com.snake.springboot.custom.api.service.base.CustPointsExpireService;
import com.snake.springboot.custom.api.service.base.CustPointsService;
import com.snake.springboot.custom.dao.mybatis.entity.base.CustPointsExpireDO;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustPointsExpireMapper;
import com.snake.springboot.custom.service.assemble.base.CustPointsExpireAssemble;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>积分过期回收业务类型
 * 类{@code CustPointsExpireServiceImpl} </br>
 * 创建时间2025/5/6 9:15
 * @author yangxt
 * @see
 */
@Service
public class CustPointsExpireServiceImpl implements CustPointsExpireService {

    private Logger logger = LoggerFactory.getLogger(CustPointsExpireServiceImpl.class);

    private CustPointsExpireMapper expireMapper;

    private CustPointsService pointsService;
    @Autowired
    public CustPointsExpireServiceImpl(CustPointsExpireMapper expireMapper, CustPointsService pointsService) {
        this.expireMapper = expireMapper;
        this.pointsService = pointsService;
    }

    @Override
    public void add(PointsChangeModel model) {
        CustPointsExpireDO changeDO = CustPointsExpireAssemble.toExpireDO(model);
        expireMapper.insertSelective(changeDO);
    }

    @Override
    public void reduce(String customId, BigDecimal changePoints) {
        changePoints=changePoints.abs();
        List<CustPointsExpireDO> expireDOList = expireMapper.queryByCustomId(customId);
        if (ListUtils.isEmpty(expireDOList)){
            return;
        }
        for(CustPointsExpireDO expireDO:expireDOList){
            BigDecimal subtract = expireDO.getPoints().subtract(expireDO.getPointsUsed());
            if (changePoints.compareTo(subtract) > 0){
                expireMapper.updateUsedPoints(expireDO.getCustPointsExpireId(),subtract,CustPointsExpireDO.StateEnum.USED_UP.getCode());
                changePoints = changePoints.subtract(subtract);
            }else if (changePoints.compareTo(subtract) < 0){
                expireMapper.updateUsedPoints(expireDO.getCustPointsExpireId(),changePoints,CustPointsExpireDO.StateEnum.NOT_RECYCLE.getCode());
                return;
            }else {
                expireMapper.updateUsedPoints(expireDO.getCustPointsExpireId(),subtract,CustPointsExpireDO.StateEnum.USED_UP.getCode());
                return;
            }
        }
    }

    @Override
    public void customPointsExpire() {
        List<CustPointsExpireDO> changeDO = expireMapper.expire();
        if (ListUtils.isEmpty(changeDO)){
            return;
        }
        for(CustPointsExpireDO expireDO:changeDO){
            try {
                expireDO.setPointsRecycle(expireDO.getPoints().subtract(expireDO.getPointsUsed()));
                expireDO.setState(CustPointsExpireDO.StateEnum.RECYCLE.getCode());
                int i = expireMapper.updatePointsRecycle(expireDO);
                if (i > 0){
                    PointsChangeModel model = CustPointsExpireAssemble.toModel(expireDO);
                    pointsService.expire(model);
                }
            }catch (Exception e){
                logger.error("积分过期回收失败:",e);
            }
        }
    }
}
